/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.engin.moves;

import static ac.lib.data.dateTime.now;
import ac.settings.dataModel;
import ac.settings.files;
import static ac.settings.rowsStatus.rowNormal;

import ac.settings.sqlQuerys;
import ac.ui.FrameOperationTemplat;
import ac.ui.utils;
import ac.uitls.propertiesOperation;
import java.awt.Component;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author empcl_000
 */
public class inventoryInput extends FrameOperationTemplat implements dataModel, files, sqlQuerys {

    private String meidField = "inventory_order_id";
    private String Tabel = TABLE_inventory_order_input;
    private String Items_Tables = TABLE_BAY_ITEMS;
    private String BillmeidField = "bill_id";
    private String BillTable = TABLE_BAY;

    private String sQLALL = SQL_InOrderAll;
    private String ReportStatus = REPORT_inOrder_status;

    propertiesOperation po = new propertiesOperation();

    ac.engin.archive.Products p = new ac.engin.archive.Products();

    public String[] getMoveItems(String id) {
        String[] r;
        r = db.getRow(Items_Tables, "*", "id='" + id + "'");
        return r;

    }

    //<editor-fold defaultstate="collapsed" desc="Process">
    public int chekLimit() {

        int r = 0;
        return r;
    }

    public int doProcess(String BillId) {
        int r = 0;
        ///edit
        if (p.ChckMaxLimtTabel(BillId, Items_Tables) == 1) {
            p.addTobalanceTabel(BillId, Items_Tables, "doProcess");
            r = p.addTobalanceInventoryTabel(BillId, Items_Tables);

        }

        return r;

    }

    public int reProcess(String BillId) {
        p.CutTobalanceTabel(BillId, Items_Tables, "reProcess");
        return p.CutTobalanceInventoryTabel(BillId, Items_Tables);

    }

    public int doProcess(String productID, String inventory_id, String quantity, String itemId) {
        p.addTobalance(productID, quantity, itemId, "doProcess");
        return p.addTobalanceInventory(productID, inventory_id, quantity);

    }

    public int reProcess(String productID, String inventory_id, String quantity, String item_id) {

        p.Cutbalance(productID, quantity, item_id, "reprocess");
        return p.CutbalanceInventory(productID, inventory_id, quantity);
    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Status">
    public String getStatus(String id) {
        String r;
        r = getMove(id)[6];

        return r;

    }

    public void setStatus(String id, String status) {

        String[][] usedata = {new String[]{"status", "update_status_date"}, new String[]{status, now("date", "todb")}};
        db.setUpdate(Tabel, usedata, "id='" + id + "'");
        this.setUpdatePblance(id);
    }
//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getData">
    public String getIdByCode(String code) {
        String[] r;
        r = db.getRow(Tabel, "*", "code='" + code + "'");
        return r[5];

    }

    public String[] getMove(String id) {
        String[] r;
        r = db.getRow(Tabel, "*", "id='" + id + "'");
        return r;

    }

    public String[] returnLastSellMove() {
        String[] r;
        r = db.getRow(Tabel, "*", "id='" + returnLastID() + "' ");
        return r;

    }

    public String returnLastID() {
        String[] dataMax = db.getRow(Tabel, "max(id) as m", "");
        return dataMax[0];

    }
//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Bill">
    public String getBillID(String Bid) {
        String R = "0";

        String[] ishvaeone = db.getRow(BillTable, "*", meidField + "='" + Bid + "'");
        if (ishvaeone[0] != null) {
            R = ishvaeone[0];
        }
        return R;
    }

    public int creatInventoryOrder(String Bid, Component magParent) {
        int r = 0;

        //
        String[] ishvaeone = db.getRow(BillTable, "*", meidField + "='" + Bid + "'");

        if (ishvaeone[0] != null) {
            utils.sendCreateBillError(magParent, ishvaeone[0]);
        } else {
            String[] Bdata = getMove(Bid);

            String c = BillreturnLastID();

            String[][] data
                    = new String[][]{{"code", "client_title", "client", "date", meidField}, {c, Bdata[8], Bdata[1], Bdata[3], Bid}};

            db.setInsert(BillTable, data);

            itemsFrominventoryTobill(Bid);
            c = BillreturnLastID();
            updateBillId(Bid, c);
///editabel
            ac.engin.index ix = new ac.engin.index();
            ix.BuyMoveOpen("update", c);
        }
        return r;

    }

    public void updateBillId(String bid, String orderid) {

        String[][] data = {new String[]{BillmeidField}, new String[]{orderid}};
        db.setUpdate(Tabel, data, "id='" + bid + "'");
    }

    public String BillreturnLastID() {
        String[] dataMax = db.getRow(BillTable, "max(id) as m", "");
        String r = "1";

        if (dataMax[0] != null) {

            r = dataMax[0];

        }
        return r;

    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="items">
    public String[] getItemMove(String id) {
        String[] r;
        r = db.getRow(Items_Tables, "*", "id='" + id + "'");
        return r;

    }

    public String getOrderId(String id) {
        String r;
        r = getItemMove(id)[8];
        return r;

    }

    public void itemsFrominventoryTobill(String id) {
        String n = BillreturnLastID();

        String[][] data = {new String[]{BillmeidField}, new String[]{n}};

        db.setUpdate(Items_Tables, data, meidField + "='" + id + "'");
    }

    public String deletItems(String id) {

        String[][] data = {new String[]{meidField, "del_order_id"}, new String[]{"-1", id}};

        return db.setUpdate(Items_Tables, data, meidField + "='" + id + "'");

    }

    public String resItems(String id) {

        String[][] data = {new String[]{meidField, "del_order_id"}, new String[]{id, "0"}};

        return db.setUpdate(Items_Tables, data, "del_order_id='" + id + "' and " + meidField + "='-1'");

    }

    public String returnItemsLastID() {
        String[] dataMax = db.getRow(Items_Tables, "max(id) as m", "");
        return dataMax[0];

    }

    public String getFiels(String id, String Field) {
        return db.getRow(Items_Tables, Field, "`id`='" + id + "'")[0];
    }

    public String[] getItemsMove(String id) {
        String[] r;
        r = db.getRow(Items_Tables, "*", "id='" + id + "'");
        return r;

    }

    public void setUpdatePblance(String BillId) {
        try {
            db.setOpenCon();

            ResultSet rs = db.getDataWithOutCon(Items_Tables, "*", "`inventory_order_id`='" + BillId + "'  and  rowstatus ='" + rowNormal + "'");
            while (rs.next()) {

                String[][] data = {new String[]{"product_blance_in", "product_blance"}, new String[]{"" + p.getbalance(rs.getString("product"), rs.getString("inventory")), "" + p.getbalance(rs.getString("product"))}};
                db.setUpdate(Items_Tables, data, "id='" + rs.getString("id") + "'");
            }

            db.setCloseCon();
        } catch (SQLException ex) {
            Logger.getLogger(inventoryInput.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="reportes">
    public String addTowhere = "";

    public void reportView(String url, String where, String[] updateTitles) {
        TabelString = Tabel;
        ac.reportes.reportes.updateReporteTitles(updateTitles[0],
                updateTitles[1],
                updateTitles[2],
                updateTitles[3],
                updateTitles[4]);

        setPrint(url, sQLALL + where + " " + addTowhere);
    }

    public void reportViewForPorduect(String Porduectid, Date d1, Date d2) {
        String[] reptitles = new String[5];

        String where = "";
        if (Porduectid != null && !Porduectid.equals("")) {
            String title = db.getRow(TABLE_products, "*", Integer.parseInt(Porduectid))[1];
            where = " where " + Items_Tables + ".product='" + Porduectid + "'";
            reptitles[1] = po.getWord("label.product_title") + " : " + title;

        } else {

            reptitles[1] = "";

        }

        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(Tabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }
        reptitles[0] = po.getWord("title.report_inputInventoryForProduct");
        reptitles[3] = "-";
        reptitles[4] = "-";
        this.reportView(ReportStatus, where, reptitles);
    }

    public void reportViewForClinte(String Clinte_id, Date d1, Date d2) {
        String[] reptitles = new String[5];

        String where = " where 1=1";

        if (Clinte_id != null && !Clinte_id.equals("")) {
            String clinte = db.getRow(TABLE_clients_data, "*", Integer.parseInt(Clinte_id))[1];
            reptitles[1] = po.getWord("label.client_title") + " : " + clinte;

            where += " and " + Tabel + ".client='" + Clinte_id + "'";

        } else {

            reptitles[1] = "-";

        }

        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(Tabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }
        reptitles[0] = po.getWord("title.report_inputInventoryForProduct");
        reptitles[3] = "-";
        reptitles[4] = "-";
        this.reportView(ReportStatus, where, reptitles);
    }

    public void reportViewForPorduectClinte(String Porduectid, String Clinte_id, Date d1, Date d2) {
        String[] reptitles = new String[5];

        String where = " where 1=1";

        if (Porduectid != null && !Porduectid.equals("")) {
            String title = db.getRow(TABLE_products, "*", Integer.parseInt(Porduectid))[1];
            where = " and  " + Items_Tables + ".product='" + Porduectid + "' ";
            reptitles[3] = po.getWord("label.product_title") + " : " + title;

        } else {

        }

        if (Clinte_id != null && !Clinte_id.equals("")) {
            String clinte = db.getRow(TABLE_clients_data, "*", Integer.parseInt(Clinte_id))[1];
            reptitles[1] = po.getWord("label.client_title") + " : " + clinte;

            where += " and " + Tabel + ".client='" + Clinte_id + "'";

        } else {

            reptitles[1] = "-";

        }

        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(Tabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }
        reptitles[0] = po.getWord("title.report_inputInventoryForProduct");

        reptitles[4] = "-";
        this.reportView(ReportStatus, where, reptitles);
    }

    public void reportStatus(Date d1, Date d2) {
        String[] reptitles = new String[5];

        String where = " where 1=1 ";

        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(Tabel + ".date", d1, d2);
            reptitles[1] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[1] = "-";
        }
        reptitles[0] = po.getWord("title.reportInventoryInStatus");
        reptitles[2] = "-";

        reptitles[3] = "-";
        reptitles[4] = "-";
        this.reportView(ReportStatus, where, reptitles);
    }

    public void reportStatusForInventory(String iid, Date d1, Date d2) {
        String[] reptitles = new String[5];

        String where = " where 1=1 ";

        if (iid != null && !iid.equals("")) {
            String clinte = db.getRow(tableAddstree, "*", Integer.parseInt(iid))[1];
            reptitles[1] = po.getWord("label.inventory_title") + " : " + clinte;

            where += " and " + Items_Tables + ".inventory='" + iid + "'";

        } else {

            reptitles[1] = po.getWord("label.allInventory");

        }

        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(Tabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }
        reptitles[0] = po.getWord("title.report_outputInventoryForProduct");

        reptitles[3] = "-";
        reptitles[4] = "-";
        this.reportView(ReportStatus, where, reptitles);
    }

//</editor-fold>
}
